package com.nowcoder.community.controller;

import com.nowcoder.community.service.DataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Date;

/**
 * <p>Title: DataController</p>
 * <p>author 12427</p>
 *
 * @version ver1.0
 * @date 2022/8/23 18:48
 */
@Controller
public class DataController {

    @Autowired
    private DataService dataService ;

    // 打开统计页面
    // 支持 get 和 post ， 为了和下面的forward统一
    @RequestMapping(path = "/data" , method = {RequestMethod.GET , RequestMethod.POST})
    public String getDataPage(){
        return "/site/admin/data" ;
    }

    // 统计网站UV的请求
    @RequestMapping(path = "/data/uv" , method = RequestMethod.POST)
    public String getUV(@DateTimeFormat(pattern = "yyyy-MM-dd") Date start ,
                        @DateTimeFormat(pattern = "yyyy-MM-dd")Date end , Model model){
        long uv = dataService.calculateUV(start , end) ;
        model.addAttribute("uvResult" , uv) ;
        model.addAttribute("uvStartDate" , start) ;
        model.addAttribute("uvEndDate" , end) ;
        // forward 好处是，可以复用 getDataPage 的逻辑，虽然这里没有什么逻辑
        return "forward:/data";
    }

    // 统计网站UV的请求
    @RequestMapping(path = "/data/dau" , method = RequestMethod.POST)
    public String getDAU(@DateTimeFormat(pattern = "yyyy-MM-dd") Date start ,
                        @DateTimeFormat(pattern = "yyyy-MM-dd")Date end , Model model){
        long dau = dataService.calculateDAU(start , end) ;
        model.addAttribute("dauResult" , dau) ;
        model.addAttribute("dauStartDate" , start) ;
        model.addAttribute("dauEndDate" , end) ;
        return "forward:/data";
    }


}
